package com.chinacoal.hr.levelimpl.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chinacoal.hr.levelapi.client.BizTravelClient;
import com.chinacoal.hr.levelapi.client.response.Result;
import com.chinacoal.hr.levelapi.client.response.ResultGenerator;
import com.chinacoal.hr.levelapi.entity.BizTravel;
import com.chinacoal.hr.levelapi.entity.BizTravelEmp;
import com.chinacoal.hr.levelimpl.service.BizTravelEmpService;
import com.chinacoal.hr.levelimpl.service.BizTravelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: YunTao.Li
 * @create: 2019-11-19 16:19
 * @description: 出差申请单controller
 **/
@RestController
public class BizTravelController implements BizTravelClient{

    @Autowired
    private BizTravelService bizTravelService;

    @Autowired
    private BizTravelEmpService bizTravelEmpService;

    /**
     * 查询出差申请单列表
     *
     * @param page           :
     * @param limit          :
     * @param ouId           :
     * @param bizTravelCode  :
     * @param businessStatus :
     * @param queryRange     :查询范围（all、dept、person）
     * @return : com.chinacoal.hr.levelapi.response.Result
     * @author : YunTao.Li
     * @date : 2019/11/19 2019/11/19
     */

    public Result list(long page,
                       long limit,
                       String ouId,
                       String deptCode,
                       String deptName,
                       String empName,
                       String empCode,
                       String bizTravelCode,
                       String businessStatus,
                       String queryRange) {

        // 构建查询条件
        Map<String, Object> condition = new HashMap<String, Object>();
        condition.put("page", page);
        condition.put("limit", limit);
        condition.put("orgId", ouId);
        condition.put("deptCode", deptCode);
        condition.put("deptName", deptName);
        condition.put("empName", empName);
        condition.put("empCode", empCode);
        condition.put("bizTravelCode", bizTravelCode);
        condition.put("businessStatus", businessStatus);
        condition.put("queryRange", queryRange);

        IPage<BizTravel> recordList = null;
        try {
            recordList = bizTravelService.getBizTravelList(condition);
        } catch (Throwable e) {
            return ResultGenerator.genFailedResult(e.getMessage());
        }
        return ResultGenerator.genOkResult(recordList);
    }

    /**
     * 查询出差单的随同人员
     *
     * @param travelId :
     * @return : com.chinacoal.hr.levelapi.response.Result
     * @author : YunTao.Li
     * @date : 2019/11/28 2019/11/28
     */
    public Result listEmp(String travelId) {
        List<BizTravelEmp> bizTravelEmps = null;
        try {
            bizTravelEmps = this.bizTravelEmpService.getBizTravelEmpsByTravelId(travelId);
        } catch (Throwable e) {
            e.printStackTrace();
            return ResultGenerator.genFailedResult(e.getMessage());
        }

        return ResultGenerator.genOkResult(bizTravelEmps);
    }

    /**
     * 新增出差申请单
     *
     * @param record :
     * @return : com.chinacoal.hr.levelapi.response.Result
     * @author : YunTao.Li
     * @date : 2019/11/19 2019/11/19
     */
    public Result add( BizTravel record) {
        Map<String, String> result = bizTravelService.insertBizTravel(record);

        if ("-1".equals(result.get("code"))) {
            String message = result.get("message");
            return ResultGenerator.genFailedResult(message);
        } else {
            return ResultGenerator.genOkResult();
        }
    }

    /**
     * 编辑出差申请单
     *
     * @param bizTravelId :
     * @param record      :
     * @return : com.chinacoal.hr.levelapi.response.Result
     * @author : YunTao.Li
     * @date : 2019/11/19 2019/11/19
     */
    public Result update( String bizTravelId,
                          BizTravel record) {
        Map<String, String> result = null;
        try {
            result = bizTravelService.updateBizTravel(record);
        } catch (Throwable e) {
            e.printStackTrace();
            return ResultGenerator.genFailedResult(e.getMessage());
        }

        return ResultGenerator.genOkResult();
    }


    /**
     * 删除出差申请单
     *
     * @param bizTravelId :
     * @return : com.chinacoal.hr.levelapi.response.Result
     * @author : YunTao.Li
     * @date : 2019/11/19 2019/11/19
     */
    public Result delete( String bizTravelId) {
        Map<String, String> result = bizTravelService.deleteBizTravel(bizTravelId);

        if ("-1".equals(result.get("code"))) {
            String message = result.get("message");
            return ResultGenerator.genFailedResult(message);
        } else {
            return ResultGenerator.genOkResult();
        }
    }

    /**
     * 提交出差申请单
     *
     * @param bizTravelId :
     * @param record      :
     * @return : com.chinacoal.hr.levelapi.response.Result
     * @author : YunTao.Li
     * @date : 2019/11/19 2019/11/19
     */

    public Result submit( String bizTravelId,
                          BizTravel record) {
        try {
            Map<String, String> result = bizTravelService.submitBizTravel(record);
            if ("-1".equals(result.get("code"))) {
                String message = result.get("message");
                return ResultGenerator.genFailedResult(message);
            } else {
                return ResultGenerator.genOkResult();
            }
        } catch (Throwable e) {
            return ResultGenerator.genFailedResult(e.getMessage());
        }
    }

    /**
     * 撤回出差申请单
     *
     * @param bizTravelId :
     * @param record      :
     * @return : com.chinacoal.hr.levelimpl.response.Result
     * @author : YunTao.Li
     * @date : 2019/12/12 2019/12/12
     */
    public Result drawback( String bizTravelId,
                            BizTravel record) {
        try {
            Map<String, String> result = bizTravelService.drawbackBizTravel(record);
            if ("-1".equals(result.get("code"))) {
                String message = result.get("message");
                return ResultGenerator.genFailedResult(message);
            } else {
                return ResultGenerator.genOkResult();
            }
        } catch (Throwable e) {
            return ResultGenerator.genFailedResult(e.getMessage());
        }
    }

    /**
     * 变更出差申请单
     *
     * @param bizTravelId :
     * @param record      :
     * @return : com.chinacoal.hr.levelapi.response.Result
     * @author : YunTao.Li
     * @date : 2019/12/4 2019/12/4
     */
    public Result change( String bizTravelId,
                          BizTravel record) {
        Map<String, String> result = null;
        try {
            result = bizTravelService.changeBizTravel(record);
        } catch (Throwable e) {
            e.printStackTrace();
            return ResultGenerator.genFailedResult(e.getMessage());
        }

        return ResultGenerator.genOkResult();
    }
}
